package com.hospital.hms.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import com.hospital.hms.utils.PageUtils;
import com.hospital.hms.common.CommonResult;
import com.hospital.hms.controller.form.*;
import com.hospital.hms.pojo.MedicalDept;
import com.hospital.hms.service.MedicalDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/medical/dept")
@Tag(name = "MedicalDeptController", description = "科室管理接口")

public class MedicalDeptController {
    @Autowired
    private MedicalDeptService medicalDeptService;


    @Operation(summary = "分页显示科室信息接口")
    @PostMapping("/selectConditionByPage")
    @SaCheckPermission(value = {"ROOT", "MEDICAL_DEPT:SELECT"}, mode = SaMode.OR)
    public CommonResult selectConditionByPage(@RequestBody @Valid SelectMedicalDeptByPageForm form) {

        Map<String, Object> map = BeanUtil.beanToMap(form);
        //获取当前页，假设是1
        int page = form.getPage();
        //获取每页显示多少条数据，假设是10
        int length = form.getLength();
        // 当前页 - 1 * 每页显示多少条 得到数据库查询的起始索引
        int start = (page - 1) * length;
        map.put("start", start);
        PageUtils pageUtils = medicalDeptService.selectConditionByPage(map);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT,pageUtils);
    }
    @Operation(summary = "添加科室信息接口")
    @PostMapping("/insert")
    @SaCheckPermission(value = {"ROOT", "MEDICAL_DEPT:INSERT"}, mode = SaMode.OR)
    public CommonResult insert(@RequestBody  @Valid InsertMedicalDeptForm form){
        MedicalDept entity = BeanUtil.toBean(form, MedicalDept.class);
        medicalDeptService.insert(entity);
        return CommonResult.ok();
    }
    @Operation(summary = "通过ID查询科室信息接口")
    @PostMapping("/selectById")
    @SaCheckPermission(value = {"ROOT", "MEDICAL_DEPT:SELECT"}, mode = SaMode.OR)
    public CommonResult selectById(@RequestBody @Valid SelectMedicalDeptByIdForm form) {
        HashMap map = medicalDeptService.selectById(form.getId());
        return CommonResult.ok(map);
    }

    @Operation(summary = "修改科室信息接口")
    @PostMapping("/update")
    @SaCheckPermission(value = {"ROOT", "MEDICAL_DEPT:UPDATE"}, mode = SaMode.OR)
    public CommonResult update(@RequestBody @Valid UpdateMedicalDeptForm form) {
        MedicalDept entity = BeanUtil.toBean(form, MedicalDept.class);
        medicalDeptService.update(entity);
        return CommonResult.ok();
    }

    @Operation(summary = "删除科室信息接口")
    @PostMapping("/deleteByIds")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "MEDICAL_DEPT:DELETE"}, mode = SaMode.OR)
    public CommonResult deleteByIds(@RequestBody @Valid DeleteMedicalDeptByIdsForm form) {
        medicalDeptService.deleteByIds(form.getIds());
        return CommonResult.ok();
    }
    @Operation(summary = "获取所有科室名称和id信息接口")
    @PostMapping("/selectAllDeptNameAndId")
    @SaCheckPermission(value = {"ROOT", "MEDICAL_DEPT:UPDATE"}, mode = SaMode.OR)
    public CommonResult selectAllDeptNameAndId(){
        ArrayList<HashMap> params = medicalDeptService.selectAllDeptNameAndId();
        return CommonResult.ok().put(CommonResult.RETURN_RESULT,params);
    }

    @Operation(summary = "获取所有科室门诊信息")
    @PostMapping("/selectDeptAndSub")
    @SaCheckLogin
    public CommonResult selectDeptAndSub(){

        LinkedHashMap<String, ArrayList<HashMap<String, Object>>> map = medicalDeptService.selectDeptAndSub();

        return CommonResult.ok().put(CommonResult.RETURN_RESULT,map);
    }

}
